package com.example.tushushibie.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.tushushibie.entity.Book;
import com.example.tushushibie.entity.BookInfo;
import com.example.tushushibie.service.BookInfoService;
import com.example.tushushibie.service.OssService;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.ocr.v20181119.OcrClient;
import com.tencentcloudapi.ocr.v20181119.models.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;

@RestController
@RequestMapping("shibie")
public class ShibieController {

    @Autowired
    BookInfoService bookInfoService;
    @Autowired
    OssService ossService;
//    Credential cred = new Credential("AKIDSklV0EBqEMDm57sLXT8WcR0IJ2UfZBXc", "pNeeNoyrTnqd9NK0PWMvDEyRdYCYdS1K");
    @PostMapping("shibie")
    public Book shibie(MultipartFile file) {
        GeneralBasicOCRResponse resp=new GeneralBasicOCRResponse();

        String bookurl = ossService.uploadFileAvatar(file);
        Book bookfianlly=new Book();
        try{
            // 实例化一个认证对象，入参需要传入腾讯云账户secretId，secretKey,此处还需注意密钥对的保密
            // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
            Credential cred = new Credential("AKIDSklV0EBqEMDm57sLXT8WcR0IJ2UfZBXc", "pNeeNoyrTnqd9NK0PWMvDEyRdYCYdS1K");
            // 实例化一个http选项，可选的，没有特殊需求可以跳过
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("ocr.tencentcloudapi.com");
            // 实例化一个client选项，可选的，没有特殊需求可以跳过
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            // 实例化要请求产品的client对象,clientProfile是可选的
            OcrClient client = new OcrClient(cred, "ap-beijing", clientProfile);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            GeneralBasicOCRRequest req = new GeneralBasicOCRRequest();
            req.setImageUrl(bookurl);
            // 返回的resp是一个GeneralBasicOCRResponse的实例，与请求对象对应
             resp = client.GeneralBasicOCR(req);
            // 输出json格式的字符串回包
//            String respjson= JSON.toJSONString(resp);
            String bookName,author;
            int width,height;
//            Book bookfianlly=new Book();
            Book book=new Book();
            String detectedText;
            int flag=0;
            long size ;
            for (int i = 0; i < resp.getTextDetections().length; i++) {
//            for (int i = 0; i < 4; i++) {
                TextDetection textDetection =resp.getTextDetections()[i];
                if (textDetection.getConfidence()>90){
                    System.out.println("this"+i+" :"+textDetection.getDetectedText());
                    detectedText=textDetection.getDetectedText();
//                    System.out.println("this "+i+"width :"+textDetection.getItemPolygon().getWidth());
//                    System.out.println("this "+i+" height :"+textDetection.getItemPolygon().getHeight());
//                    System.out.println("this "+i+"  X ："+textDetection.getItemPolygon().getX());
//                    System.out.println("this "+i+"  Y ："+textDetection.getItemPolygon().getY());
//                    System.out.println("this "+i+"  可信度 :"+textDetection.getConfidence());
                    size=textDetection.getItemPolygon().getWidth()*textDetection.getItemPolygon().getHeight();

                    if (size>2000){
                        System.out.println("sizez;"+size);


                        QueryWrapper wrapper=new QueryWrapper();
                        wrapper.like("book_name",detectedText);

//                    wrapper.like("author",detectedText);
                        BookInfo bookInfo = bookInfoService.getBaseMapper().selectOne(wrapper);
                        if (bookInfo!=null){
                            System.out.println(bookInfo.toString());
                            book.setBookName(bookInfo.getBookName());
                            book.setAuthor(bookInfo.getAuthor());
                            book.setPulishingHouse(bookInfo.getPulishingHouse());
                        }
                        QueryWrapper wrapper1=new QueryWrapper();
                        wrapper1.eq("author",detectedText);
                        BookInfo bookInfo1 = bookInfoService.getBaseMapper().selectOne(wrapper);
                        Book book2=new Book();
                        if (bookInfo1!=null){
                            book2.setAuthor(bookInfo1.getAuthor());
                            book2.setBookName(bookInfo1.getBookName());
                            book2.setPulishingHouse(bookInfo1.getPulishingHouse());
                        }
                        if (book.equals(book2)){
                            flag=flag+1;
                            bookfianlly=book;
                        }
                    }


                }
            }

            System.out.println("bookfinlly:"+bookfianlly.toString()+"flage:  "+flag);


            System.out.println(GeneralBasicOCRResponse.toJsonString(resp));
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
        }
        return bookfianlly;
    }

    @PostMapping("shibiemany")
    public Book[] shibie(MultipartFile[] files ){

        Book[] book=new Book[files.length];
        for (int i = 0; i < files.length; i++) {
            Book shibie = shibie(files[i]);
            book[i]=shibie;
        }

     return book;
    }


    @GetMapping("test")
    public String test(){
        return "aaa";
    }

}
